import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-09-21
 * Time: 10:05
 */
public class MyQueue {
    //双向链表实现队列
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode first = null;
    public ListNode last = null;

    public int usedSize = 0;

    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(isEmpty()){
            first = last = node;
            usedSize++;
            return;
        }
        node.prev = last;
        last.next = node;
        last = node;
        usedSize++;
    }

    public int poll() {
        if(isEmpty()) {
            throw new EmptyQueueException();
        }
        int valPoll = first.val;
        first = first.next;
        if(first != null) {
            first.prev = null;
        }
        usedSize--;
        return valPoll;
    }

    public int peek() {
        if(isEmpty()) {
            throw new EmptyQueueException();
        }
        return first.val;
    }

    public boolean isEmpty() {
        return usedSize == 0;
        //return first == 0;
    }
}
